#include "vars.h"
#include "actions.h"
#include "ui.h"
#include "screens.h"
#include <esp_log.h>

#include "freertos/FreeRTOS.h"  // 添加FreeRTOS头文件
#include "freertos/task.h"      // 添加任务管理头文件

#include "boards/startnorva/sd_card.h"
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
#include "driver/sdmmc_host.h"

#include <errno.h>  
#include <string.h>
#include <time.h>  

#include "src/misc/lv_timer.h"

#include "boards/startnorva/CJ_display.h"
#include "application.h"


#include "display.h"
#include "board.h"
#include "screens.h"
#include "wifi_station.h"

#include "wifi_board.h"
#include "esp_lvgl_port.h"







static const char *TAG = "action";

#ifdef __cplusplus
extern "C" {
#endif
#define ANIMATION_DURATION_MS 100 //切片动画持续时间（毫秒）
#define SWIPE 20       // 滑动触发阈值（像素）


static void tick_app1() {

    // 获取当前系统时间
    time_t now = time(nullptr);
    if(now == (time_t)-1) return;
    
    struct tm timeinfo;
    if(localtime_r(&now, &timeinfo) == nullptr) return;
    
    // 更新主时间标签 (app2_time1)
    if (objects.app1_time1) {
        char time_str[20];
        if(strftime(time_str, sizeof(time_str), "%H:%M:%S", &timeinfo) > 0) {
            
            lvgl_port_lock(0);
            lv_label_set_text_fmt(objects.app1_time1, "%02d:%02d", timeinfo.tm_hour, timeinfo.tm_min);
            lvgl_port_unlock();
        }
    }

    // 更新秒数标签 (app2_time2)
    if (objects.app1_time2) {
        lvgl_port_lock(0);
        lv_label_set_text_fmt(objects.app1_time2, "%02d", timeinfo.tm_sec);
        lvgl_port_unlock();
    }

}


static void tick_app2() {

    // 获取当前系统时间
    time_t now = time(nullptr);
    if(now == (time_t)-1) return;
    
    struct tm timeinfo;
    if(localtime_r(&now, &timeinfo) == nullptr) return;
    
    // 更新主时间标签 (app2_time1)
    if (objects.app2_time1) {
        char time_str[20];
        if(strftime(time_str, sizeof(time_str), "%H:%M:%S", &timeinfo) > 0) {
            
            lvgl_port_lock(0);
            lv_label_set_text_fmt(objects.app2_time1, "%02d:%02d", timeinfo.tm_hour, timeinfo.tm_min);
            lvgl_port_unlock();
        }
    }

    // 更新秒数标签 (app2_time2)
    if (objects.app2_time2) {
        lvgl_port_lock(0);
        lv_label_set_text_fmt(objects.app2_time2, "%02d", timeinfo.tm_sec);
        lvgl_port_unlock();
    }

}




static void uiTickTask(void* arg) {

    uint32_t last_second = 0;
    uint32_t last_wake_time = xTaskGetTickCount();

    while(1) {
    
        // 只在秒数变化时更新UI
        time_t now = time(nullptr);
        if(now != (time_t)-1) {
            struct tm timeinfo;
            if(localtime_r(&now, &timeinfo) != nullptr) {
                if(timeinfo.tm_sec != last_second) {
                    last_second = timeinfo.tm_sec;                  
                    lvgl_port_lock(0);
                    tick_app1();
                    tick_app2();
                    lvgl_port_unlock();                
          
                }
            }
        }

        vTaskDelayUntil(&last_wake_time, pdMS_TO_TICKS(100));
    }

}


// 在合适的地方（如应用初始化函数中）创建任务
void create_clock_task(void) {

    // 创建 UI 定时任务
    TaskHandle_t ui_task_handle = NULL;
    BaseType_t ret = xTaskCreate(
        uiTickTask,              
        "ui_tick_task",          
        4096,                  
        NULL,                     
        2,                       
        &ui_task_handle           
    );
    
    if (ret != pdPASS) {
        // 任务创建失败处理
        ESP_LOGE("TASK", "创建 UI 任务失败");
    }
}







void action_main_gesture(lv_event_t * e)
{
    ESP_LOGI(TAG, "action_main_gesture called");
    lv_indev_t * indev = lv_indev_get_act();
    if(indev) {
        lv_point_t vect;
        lv_indev_get_vect(indev, &vect);

        if(vect.x < -SWIPE) {          
            lv_screen_load_anim(objects.app1_page, LV_SCR_LOAD_ANIM_MOVE_LEFT, ANIMATION_DURATION_MS, 0, false);           
        }
        
        if(vect.x > SWIPE) {
            lv_screen_load_anim(objects.app3_page, LV_SCR_LOAD_ANIM_MOVE_RIGHT, ANIMATION_DURATION_MS, 0, false);
        }  
    }
}




void action_app1_gesture(lv_event_t * e)
{
    ESP_LOGI(TAG, "action_app1_gesture called");
    lv_indev_t * indev = lv_indev_get_act();
    if(indev) {
        lv_point_t vect;
        lv_indev_get_vect(indev, &vect);

        if(vect.x < -SWIPE) {          
            lv_screen_load_anim(objects.app2_page, LV_SCR_LOAD_ANIM_MOVE_LEFT, ANIMATION_DURATION_MS, 0, false);           
        }
        
        if(vect.x > SWIPE) {
            lv_screen_load_anim(objects.main, LV_SCR_LOAD_ANIM_MOVE_RIGHT, ANIMATION_DURATION_MS, 0, false);
        }  
    }

}
void action_app2_gesture(lv_event_t * e)
{
    ESP_LOGI(TAG, "action_app2_gesture called");
    lv_indev_t * indev = lv_indev_get_act();
    if(indev) {
        lv_point_t vect;
        lv_indev_get_vect(indev, &vect);

        if(vect.x < -SWIPE) {          
            lv_screen_load_anim(objects.app3_page, LV_SCR_LOAD_ANIM_MOVE_LEFT, ANIMATION_DURATION_MS, 0, false);           
        }
        
        if(vect.x > SWIPE) {
            lv_screen_load_anim(objects.app1_page, LV_SCR_LOAD_ANIM_MOVE_RIGHT, ANIMATION_DURATION_MS, 0, false);
        }  
    }

}


void action_app3_gesture(lv_event_t * e)
{
    ESP_LOGI(TAG, "action_app3_gesture called");
    lv_indev_t * indev = lv_indev_get_act();
    if(indev) {
        lv_point_t vect;
        lv_indev_get_vect(indev, &vect);

        if(vect.x < -SWIPE) {          
            lv_screen_load_anim(objects.main, LV_SCR_LOAD_ANIM_MOVE_LEFT, ANIMATION_DURATION_MS, 0, false);           
        }
        
        if(vect.x > SWIPE) {
            lv_screen_load_anim(objects.app2_page, LV_SCR_LOAD_ANIM_MOVE_RIGHT, ANIMATION_DURATION_MS, 0, false);
        }  
    }

}

void ai_clock() {

    lvgl_port_lock(0);
    // create_screen_page_game_task();
    lv_scr_load(objects.app1_page);
    lvgl_port_unlock();

}




#ifdef __cplusplus
}
#endif
